package com.itheima.dao.impl;

import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("roleDao")
public class RoleDaoImpl implements RoleDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Role> findAll() {
        String sql = "select * from sys_role";
        List<Role> roles = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Role>(Role.class));
        return roles;
    }

    @Override
    public void save(Role role) {
        String sql = "insert into sys_role VALUES(?,?,?)";
        jdbcTemplate.update(sql,null,role.getRoleName(),role.getRoleDesc());

    }

    @Override
    public List<String> findRolesNameByUserID(Long id) {
        String sql = "SELECT r.roleName from sys_user_role ur,sys_role r where ur.roleId = r.id and ur.userId = ?";
        List<String> list = jdbcTemplate.queryForList(sql,String.class, id);
        System.out.println(list);
        return list;
    }
}
